/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AgenciaView.java
 *
 * Created on 27/06/2009, 17:20:25
 */

package com.celiosilva.simbanc.view;

import com.celiosilva.simbanc.arquitetura.AcessoValidavel;
import com.celiosilva.simbanc.arquitetura.Sessao;
import com.celiosilva.simbanc.beans.Agencia;
import com.celiosilva.simbanc.business.AgenciaBusiness;
import com.celiosilva.simbanc.business.seguranca.Permissao;
import com.celiosilva.simbanc.business.util.Sucesso;
import com.celiosilva.simbanc.controller.util.Mensagem;
import com.celiosilva.simbanc.exceptions.AcessoNegadoException;
import com.celiosilva.simbanc.exceptions.RegraNegocioException;
import com.celiosilva.simbanc.exceptions.SimbancException;
import com.celiosilva.simbanc.exceptions.SistemaException;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public abstract class AgenciaView extends javax.swing.JInternalFrame {

    /** Creates new form AgenciaView */
    public AgenciaView() {
        initComponents();
        this.setTitle(this.getTitulo());
        this.setEstadoInicial();
    }

    protected abstract String getTitulo();
    protected abstract void setEstadoInicial();

    protected class VisualizarAgencia implements AcessoValidavel{
        @Override
        public Permissao getPermissaoAtribuida() {
            return Permissao.VISUALIZAR_AGENCIA;
        }
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        txtCodAgencia = new javax.swing.JTextField();
        txtNome = new javax.swing.JTextField();
        txtCNPJ = new javax.swing.JTextField();
        txtEndereco = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        btnSalvar = new javax.swing.JButton();
        btnConsultar = new javax.swing.JButton();
        btnDesativar = new javax.swing.JButton();
        btnReativar = new javax.swing.JButton();
        cbxAgenciaAtiva = new javax.swing.JCheckBox();

        setClosable(true);
        setIconifiable(true);
        setTitle("Agencia View");

        jLabel1.setText("Código Agencia:");

        jLabel2.setText("Nome:");

        jLabel3.setText("CNPJ:");

        jLabel4.setText("Endereço:");

        txtCodAgencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodAgenciaActionPerformed(evt);
            }
        });

        txtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeActionPerformed(evt);
            }
        });

        txtEndereco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEnderecoActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        btnDesativar.setText("Desativar");
        btnDesativar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDesativarActionPerformed(evt);
            }
        });

        btnReativar.setText("Reativar");
        btnReativar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReativarActionPerformed(evt);
            }
        });

        cbxAgenciaAtiva.setText("Agencia Ativa");
        cbxAgenciaAtiva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxAgenciaAtivaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtCodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnConsultar))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(37, 37, 37)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cbxAgenciaAtiva))
                                .addComponent(txtNome, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                                .addComponent(txtEndereco))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDesativar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnReativar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConsultar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxAgenciaAtiva))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvar)
                    .addComponent(btnDesativar)
                    .addComponent(btnReativar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodAgenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodAgenciaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodAgenciaActionPerformed

    private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomeActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtNomeActionPerformed

    private void txtEnderecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEnderecoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtEnderecoActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        try {
            AcessoValidavel validacao = new AcessoValidavel() {
                @Override
                public Permissao getPermissaoAtribuida() {
                    return Permissao.CADASTRAR_AGENCIA;
                }
            };
            Sessao.getInstance().validarAcesso(validacao);
            Agencia novo = this.preencherBeanAgencia(new Agencia());
            Sucesso resultado = AgenciaBusiness.getInstance().cadastrarAgencia(novo);
            this.mostrarAviso(resultado);
            this.limparTela();
        } catch (SimbancException ex) {
            this.mostrarAviso(ex);
        }
}//GEN-LAST:event_btnSalvarActionPerformed

    private void cbxAgenciaAtivaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxAgenciaAtivaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cbxAgenciaAtivaActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        try {
            VisualizarAgencia visualizar = new VisualizarAgencia();
            Sessao.getInstance().validarAcesso(visualizar);
            int codigo = Integer.parseInt(txtCodAgencia.getText());
            Agencia agencia = AgenciaBusiness.getInstance().consultarAgencia(codigo);
            this.mostrarBeanAgencia(agencia);
        } catch (AcessoNegadoException ex) {
            this.mostrarAviso(ex);
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void btnDesativarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDesativarActionPerformed
        try {
            Sessao.getInstance().validarAcesso(new AcessoValidavel() {
                @Override
                public Permissao getPermissaoAtribuida() {
                    return Permissao.CANCELAR_AGENCIA;
                }
            });
            Agencia agencia = this.preencherBeanAgencia(new Agencia());
            Sucesso sucesso = AgenciaBusiness.getInstance().desativarAgencia(agencia);
            this.mostrarAviso(sucesso);
            this.limparTela();
        } catch (SimbancException ex) {
            this.mostrarAviso(ex);
        }
    }//GEN-LAST:event_btnDesativarActionPerformed

    private void btnReativarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReativarActionPerformed
        
        class FuncaoReativarAgencia implements AcessoValidavel{
            @Override
            public Permissao getPermissaoAtribuida() {
                return Permissao.REATIVAR_AGENCIA;
            }
        }
        FuncaoReativarAgencia funcao = new FuncaoReativarAgencia();
        try {
            Sessao.getInstance().validarAcesso(funcao);
            Agencia agencia = this.preencherBeanAgencia(new Agencia());
            Sucesso sucesso = AgenciaBusiness.getInstance().reativarAgencia(agencia);
            JOptionPane.showMessageDialog(this, sucesso);
            this.limparTela();
        } catch (SimbancException ex) {
            this.mostrarAviso(ex);
        }
    }//GEN-LAST:event_btnReativarActionPerformed

    private void limparTela(){
        this.txtCNPJ.setText("");
        this.txtCodAgencia.setText("");
        this.txtEndereco.setText("");
        this.txtNome.setText("");
        this.cbxAgenciaAtiva.setSelected(false);
    }

    private Agencia preencherBeanAgencia(Agencia agencia){
        if (txtCodAgencia.getText() != null && txtCodAgencia.getText().length() > 0){
            int codigoAgencia = Integer.parseInt(txtCodAgencia.getText());
            agencia.setCodigo(codigoAgencia);
        }
        agencia.setCnpj(txtCNPJ.getText());
        agencia.setAtivo(cbxAgenciaAtiva.isSelected());
        agencia.setEndereco(txtEndereco.getText());
        agencia.setNome(txtNome.getText());
        return agencia;
    }

    private void mostrarBeanAgencia(Agencia agencia){
        this.limparTela();
        if (agencia == null)
            return;
        this.txtCNPJ.setText(agencia.getCnpj());
        this.txtCodAgencia.setText(String.valueOf(agencia.getCodigo()));
        this.txtEndereco.setText(agencia.getEndereco());
        this.txtNome.setText(agencia.getNome());
        this.cbxAgenciaAtiva.setSelected(agencia.isAtivo());
    }

    public void mostrarAviso(Mensagem aviso){
        int tipoAviso = JOptionPane.INFORMATION_MESSAGE;
        if (aviso instanceof RegraNegocioException)
            tipoAviso = JOptionPane.WARNING_MESSAGE;
        else if (aviso instanceof SistemaException)
            tipoAviso = JOptionPane.ERROR_MESSAGE;
        JOptionPane.showMessageDialog(this, aviso.getMensagem(), aviso.getClass().getSimpleName(), tipoAviso);
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JButton btnConsultar;
    protected javax.swing.JButton btnDesativar;
    protected javax.swing.JButton btnReativar;
    protected javax.swing.JButton btnSalvar;
    protected javax.swing.JCheckBox cbxAgenciaAtiva;
    protected javax.swing.JLabel jLabel1;
    protected javax.swing.JLabel jLabel2;
    protected javax.swing.JLabel jLabel3;
    protected javax.swing.JLabel jLabel4;
    protected javax.swing.JSeparator jSeparator1;
    protected javax.swing.JSeparator jSeparator2;
    protected javax.swing.JTextField txtCNPJ;
    protected javax.swing.JTextField txtCodAgencia;
    protected javax.swing.JTextField txtEndereco;
    protected javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables

}
